<template>
  <div class="login">
    <div class="content">
      <!--       头像 -->
      <div class="avator">
        <img src="~assets/img/logo.png" alt="" />
      </div>
      <!--   登录表单 -->
      <el-form
        label-width="80px"
        :rules="loginRules"
        class="form"
        :model="form"
        ref="reloginform"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="iconfont icon-yonghuming"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="iconfont icon-mima"
            type="password"
            @keydown.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" round @click.native="login">登录</el-button>
          <el-button round @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
/* import { request } from "network/request.js"; */
export default {
  name: "",
  components: {},
  data() {
    return {
      form: {
        username: "admin",
        password: "",
      },
      //表单验证规则
      loginRules: {
        username: [
          { required: true, message: "请输用户名", trigger: "blur" },
          {
            min: 4,
            max: 10,
            message: "长度在 4 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 10,
            message: "长度在 6 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      this.$refs.reloginform.resetFields();
    },
    login() {
      this.$refs.reloginform.validate(async (val) => {
        /*         console.log(val); */
        if (val) {
          //await语法
          /*  const {data:res} =await request({ url: "/login", params: this.form, method: "post" })
     
        console.log(res);
        if(res.meta.status!==200){
console.log('登录失败');
              }
             else{
                console.log('登录成功');
             } */
          this.$request({ url: "/login", params: this.form, method: "post" })
            .then((res) => {
              //console.log(res);
              let data = res.data;
              console.log(data);
              if (data.meta.status !== 200) {
                this.$message.error("登录失败");
              } else {
                this.$message({
                  message: "登录成功",
                  type: "success",
                });

                sessionStorage.setItem("token", data.data.token);
                this.$router.push("/home");
              }
            })
            .catch((err) => {
              console.log(err);
            });
          /*  const hh = await this.$axios.post("login", this.form);
          console.log(hh); */
        }
      });
    },
    loginer() {
      console.log("***************");
    },
  },
  created() {},
};
</script>
<style scoped>
.login {
  position: relative;
  width: 100%;
  height: 100%;

  background-color: darkcyan;
}
.content {
  position: absolute;
  top: 120px;
  left: 50%;
  transform: translateX(-50%);
  width: 500px;
  height: 360px;
  background-color: #ffff;
}
.avator {
  position: relative;
  left: 50%;
  padding: 12px;
  transform: translate(-50%, -50%);
  width: 120px;
  height: 120px;
  box-shadow: 0 0 8px #787879;
  border-radius: 50%;
  background: #ffff;
  border: 1px solid #eee;
}
.avator img {
  border-radius: 50%;

  background-color: #eee;
  width: 100%;
  height: 100%;
}
.form {
  position: relative;
  top: -30px;
  left: -20px;
}
.btn {
  display: flex;
  justify-content: flex-end;
}
</style>
